package tictactoe.game;
import java.util.ArrayList;
import java.util.HashMap;

public class HvC extends AbstractController
{
	 HashMap<int[][],Pair> m = new HashMap<int[][],Pair>();
	 Pair t;
	 public static int retX = 0,retY = 0;
	 private int row,col;
	 int[][] copy;
	    
	 //constructor,initialise grid
	 public HvC(Model model) 
	 {
	    super(model);
	 }
	 
	 //ask the corresponding player for choice
	 //player 2 - computer
	 //player 1 - human
	 //int row,col = 0;
	 public void play(int player)
	 {
		 
	 }
	 //make move if ok, otherwise ask again for choice of position
	 //check if game finished
	 public int makeMove(int row, int col, int sign) 
	 {
	    if (sign == 2)
	    {
	    	copy = grid;
	    	cMove(copy,1);
	    	t = m.get(grid);
	    	
	    	grid[t.f][t.s] = 2;
	    	retX = t.f;
	    	retY = t.s;
	    }
	    else
	    {
     	  if (grid[row][col] <= 0) grid[row][col] = 1;
	  	  else
	      {
	    	
	   		return -1;
	   	  }	
	    }
	    
	    
	    
	   	//show();
	    if (checkWon(grid)) 
	    { if (sign == 2) { return 2;}
	      else { return 1;}
	    }
	    else
	    if (all==true) { return 3;}
	      
	    player ^= 1;
	    return 0;
	 }

	 
	 
	 
	 private boolean checkForTwoInARow(int[][] board, int sign)
	 {
		 int br = 0;
		 for(int i=0;i<X;i++)
		 {
			 br = 0;
			 for(int j=0;j<Y;j++)
				 if (board[i][j] > 0) br++;
			 if (br == X) continue;
			 br = 0;
			 for(int j=0;j<Y;j++)
				 if (sign == board[i][j]) br++;
				 else {row = i;col = j;}
			 if (br == X-1) break;
		 }
		 if (br == X-1) return true;
		 
		 for(int j=0;j<Y;j++)
		 {
			 br = 0;
			 for(int i=0;i<X;i++)
				 if (board[i][j] > 0) br++;
			 if (br == X) continue;
			 br = 0;
			 for(int i=0;i<X;i++)
				 if (sign == board[i][j]) br++;
				 else {row = i;col = j;}
			 if (br == X-1) break;
		 }
		 if (br == X-1) return true;
		 
		 br = 0;
		 for(int i=0;i<X;i++)
			 if (board[i][i] > 0) br++;
		 if (br < X)
		 {
			 br = 0;
			 for(int i=0;i<X;i++)
				 if (sign == board[i][i]) br++;
				 else {row = i;col = i;}
			 if (br == X-1) return true;
		 }
		 
		 br = 0;
		 for(int i=0;i<X;i++)
			 if (board[i][X-i-1] > 0) br++;
		 if (br < X)
		 {
			 br = 0;
			 for(int i=0;i<X;i++)
				 if (sign == board[i][X-i-1]) br++;
				 else {row = i;col = X-i-1;}
			 if (br == X-1) return true;
		 }
		 return false;
	 }
	 
	 private boolean allUsed(int[][] board)
	 {
		 for(int i=0;i<X;i++)
		 {
			 for(int j=0;j<Y;j++)
				 if (board[i][j] < 0) return false; 
		 }
		 return true;
	 }
	 
	 private ArrayList<Pair> next_states(int[][] board)
	 {
		 ArrayList<Pair> ret = new ArrayList<Pair>();
		 for(int i=0;i<X;i++)
			 for(int j=0;j<Y;j++)
				 if (board[i][j] < 0)
				 {
					 
					 ret.add(new Pair(i,j));
					 
				 }
		 return ret;
	 }
	 
	 private int cMove(int[][] board, int player)
	 {
		int win = -100;
		int x,temp;
		
		if (checkWon(board))
		{
			if 
			(player == 1) return 1;
			else return -1;
		}
		else
		if (allUsed(board)) return 0;
		
		ArrayList<Pair> nexts = next_states(board);
		int i,k = nexts.size();
		
		if (checkForTwoInARow(board,player+1))
		{
			m.put(board, new Pair(row,col));
			win = 3*k;
			return win;
		}
		if (checkForTwoInARow(board,(player == 0)? 2 : 1))
		{
			m.put(board, new Pair(row,col));
			win = 3*k;
			return win;
		}
		
		for(i=0;i<=k-1;i++)
		{
			temp = board[nexts.get(i).f][nexts.get(i).s];
			board[nexts.get(i).f][nexts.get(i).s] = player+1;
			x = cMove(board,player^1);
			board[nexts.get(i).f][nexts.get(i).s] = temp;
			
			if (player == 1) 
			{
				if (x > win)
				{
					win = x;
					m.put(board,new Pair(nexts.get(i).f,nexts.get(i).s));
				}
			}
			else win = Math.min(win, x);
		}
		
		return win;
	 }
	 
	 
	 
	 
	
	 
	 /*
	 public static void main(String[] args) 
	 {
		 HvC game = new HvC();
	     game.play(0);
	 }
	 */

}